package game;

import game.controller.DisplayController;
import game.controller.GameController;
import game.util.GameLogger;
import game.util.GameMsg;

import javax.swing.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.*;
import java.io.IOException;

import lobby.controller.User;

/**
 * NiEGame.java  Date Created: Oct 22, 2012
 *
 * Purpose: To run the client GUI for the in game graphics.
 *
 * Description:  This class will create the game controllers and GUIs
 * that are needed to display and run the game. 
 *
 * @author Chrisb
 */
public class NiEGame {
    private JFrame frame;
    private DisplayController displayController;
    private GameController controller;

    /**
     * The Main Method
     */
    public static void main(String[] args) throws IOException {
        (new NiEGame(new User("testUser", 0, 2, "testLeader"), new byte[]{0,0})).start();
	}

    /**
     * Constructor
     */
    public NiEGame(User user, byte[] lobbyId) {
        GameLogger.createLogFile();
        GameMsg.load(); 
        //Create the window with the given title
//        frame = new JFrame(Messages.getString("frame.title"));
        frame = new JFrame("Napoleon In Europe");
//        frame.setUndecorated(true);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        controller = new GameController(user, lobbyId);
        displayController = new DisplayController(frame, controller);
        controller.setDisplayController(displayController);
        controller.requestGameInfo();

        while (!controller.isGameReady()) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        controller.init();
        displayController.init();
    }

	public void start() {
		SwingUtilities.invokeLater(new UIStarter());
	}

	private class UIStarter implements Runnable {
		public void run() {
			//Initial window size, in pixels
			frame.pack();

            //Initial window location, in pixels
			frame.setLocation(0,0);

            //Add window listener to do clean up when application is closed.
            frame.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent we) {
                  //controller.cleanUp();
                    frame.dispose();
                }
            });

            //Sets the window to maximized
//		    frame.setExtendedState(Frame.MAXIMIZED_BOTH);
//			int height = (int)(Toolkit.getDefaultToolkit().getScreenSize().height*.9);
//			int width = (int)(Toolkit.getDefaultToolkit().getScreenSize().width*.9);
//			frame.setSize(1920/2,1200/2);

//            frame.setExtendedState(Frame.MAXIMIZED_BOTH);



            //Prevents the frame from being resized
			frame.setResizable(false);


            //Display the window
            frame.setVisible(true);
        }
    }
}